package com.woniuxy.cloud.carl.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.carl.carlgoods.client.SkuQueryClient;
import com.woniuxy.cloud.carl.carlgoods.client.dto.SkuDto;
import com.woniuxy.cloud.carl.order.app.KdniaoTrackQueryAPI;
import com.woniuxy.cloud.carl.order.app.utils.TransInfoUtil;
import com.woniuxy.cloud.carl.order.client.ExpressQueryClient;
import com.woniuxy.cloud.carl.order.client.param.GetServiceLogByConditionParam;
import com.woniuxy.cloud.carl.order.client.param.GetTransByConditionParam;
import com.woniuxy.cloud.carl.order.client.result.ServiceLogDto;
import com.woniuxy.cloud.carl.order.client.result.TransItemDto;
import com.woniuxy.cloud.carl.order.common.ServiceStateEnum;
import com.woniuxy.cloud.carl.order.common.ServiceTypeEnum;
import com.woniuxy.cloud.carl.order.common.TransStatusEnum;
import com.woniuxy.cloud.carl.order.infra.model.ServiceLog;
import com.woniuxy.cloud.carl.order.infra.model.TransItem;
import com.woniuxy.cloud.carl.order.infra.model.UserDelivery;
import com.woniuxy.cloud.carl.order.service.ServiceLogService;
import com.woniuxy.cloud.carl.order.service.TransItemService;
import com.woniuxy.cloud.carl.order.service.UserDeliveryService;
import com.woniuxy.cloud.carl.user.client.UserQueryClient;
import com.woniuxy.cloud.carl.user.client.param.GetByConditionParam;
import com.woniuxy.cloud.carl.user.client.result.UserDetailDto;
import com.woniuxy.cloud.carl.user.client.result.UserForStationDto;
import com.woniuxy.cloud.carl.user.client.result.WoniuPageDto;
import com.woniuxy.result.Result;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ExpressQueryApiImpl implements ExpressQueryClient {
    @Autowired
    KdniaoTrackQueryAPI kdniaoTrackQueryAPI;
    @Autowired
    UserDeliveryService userDeliveryService;
    @Autowired
    TransItemService transItemService;
    @Autowired
    SkuQueryClient skuQueryClient;
    @Autowired
    ServiceLogService serviceLogService;
    @Autowired
    UserQueryClient userQueryClient;
    @Override
    public Result getOneDetail(Long userId, Long orderItemId) {
        QueryWrapper<UserDelivery> wrapper = new QueryWrapper<>();
        if (userId!=null)
            wrapper.eq("user_id", userId);
        if (orderItemId!=null)
            wrapper.eq("order_item_id", orderItemId);
        UserDelivery one = userDeliveryService.getOne(wrapper);
        Object transInfo = TransInfoUtil.getTransInfo(one.getDeliveryNo(), one.getDeliveryName());
        return Result.success(transInfo);
    }

//    @Override
//    public Result getOneTrans(Long transItemId) {
//        return null;
//    }

    @Override
    public Result<List<TransItemDto>> getTransByCondition(GetTransByConditionParam param) {
        QueryWrapper<TransItem> wrapper = new QueryWrapper<>();
        if (param.getCreateDate()!=null)
            wrapper.eq("create_date",param.getCreateDate());
        if (param.getTransStatus()!=null)
            wrapper.eq("trans_status",param.getTransStatus());
        if (param.getDeliveryNo()!=null)
            wrapper.eq("delivery_no",param.getDeliveryNo());
        if (param.getStoreId()!=null)
            wrapper.eq("store_id",param.getStoreId());
        if (param.getSkuId()!=null)
            wrapper.eq("sku_Id",param.getSkuId());
        List<TransItem> list = transItemService.list(wrapper);
        ArrayList<TransItemDto> transItemDtos = new ArrayList<>();
        for (TransItem transItem : list) {
            TransItemDto transItemDto = BeanUtil.copyProperties(transItem, TransItemDto.class);
            Result<SkuDto> skuById = skuQueryClient.findSkuById(transItemDto.getSkuId());
            transItemDto.setSkuName(skuById.getData().getSkuName());
            TransStatusEnum anEnum = TransStatusEnum.getEnum(transItemDto.getTransStatus());
            transItemDto.setTransStatusName(anEnum.getStatus());
            transItemDtos.add(transItemDto);
        }
        return Result.success(transItemDtos);
    }

//    @Override
//    public Result getAllServiceLog() {
//        return null;
//    }

    @Override
    public Result<List<ServiceLogDto>> getServiceLogByStoreId(GetServiceLogByConditionParam param) {
        QueryWrapper<ServiceLog> wrapper = new QueryWrapper<>();
        if (param.getServiceState()!=null)
            wrapper.eq("service_state",param.getServiceState());
        if (param.getServiceType()!=null)
            wrapper.eq("service_type",param.getServiceType());
        if (param.getCarNum()!=null && !param.getCarNum().equals(""))
            wrapper.eq("car_num",param.getCarNum());
        if (param.getUserName()!=null) {
            GetByConditionParam get = new GetByConditionParam();
            get.setUserName(param.getUserName());
            Result<WoniuPageDto<UserForStationDto>> byCondition = userQueryClient.getByCondition(get);
            if (byCondition.getData().getList()==null||byCondition.getData().getList().size()==0)
                wrapper.eq("user_id",0);
            else wrapper.eq("user_id",byCondition.getData().getList().get(0).getUserId());
        }
        if (param.getStaffId()!=null)
            wrapper.eq("staff_id",param.getStaffId());
        List<ServiceLog> list = serviceLogService.list(wrapper);
        ArrayList<ServiceLogDto> serviceLogDtos = new ArrayList<>();
        for (ServiceLog serviceLog : list) {
            ServiceLogDto serviceLogDto = BeanUtil.copyProperties(serviceLog, ServiceLogDto.class);
            Result<UserDetailDto> userDetail = userQueryClient. getUserDetail(serviceLog.getUserId().longValue());
            serviceLogDto.setUserName(userDetail.getData().getUserName());
            ServiceStateEnum anEnum = ServiceStateEnum.getEnum(serviceLog.getServiceState());
            serviceLogDto.setServiceStateName(anEnum.getStatus());
            ServiceTypeEnum anEnum1 = ServiceTypeEnum.getEnum(serviceLog.getServiceType());
            serviceLogDto.setServiceTypeName(anEnum1.getStatus());
            serviceLogDtos.add(serviceLogDto);
        }

        return Result.success(serviceLogDtos);
    }
}
